<!--
 * @Author: DUST
 * @Date: 2023-05-16 16:33:19
 * @LastEditors: DUST
 * @LastEditTime: 2023-06-28 13:34:31
 * @FilePath: \vite-project\src\views\admin\components\addNoteModal.vue
 * @Description: 
 * undefined
-->
<template>
  <ant-modal
    :visible="visible"
    :type="type"
    :loading="loading"
    :width="1000"
    :title="title"
    @confirm="doConfirm"
    @cancel="cancel"
  >
    <a-form
      ref="formref"
      :model="formModel"
      class="ModalForm"
      :labelCol="{ span: 2 }"
      :wrapperCol="{ span: 20 }"
    >
      <a-form-item
        label="标题"
        :rules="[{ required: true, message: '标题不能为空' }]"
        name="title"
      >
        <a-input placeholder="请输入标题" v-model:value="formModel.title" />
      </a-form-item>
      <a-form-item label="标签" name="tagIds">
        <a-select
          v-model:value="formModel.tagIds"
          mode="multiple"
          style="width: 100%"
          placeholder="请选择此次笔记相关标签"
        >
          <a-select-option :value="item.id" v-for="item of tagList" :key="item.id">
            {{ item.name }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="简述" name="description">
        <a-input
          placeholder="请输入简述内容"
          v-model:value="formModel.description"
        />
      </a-form-item>
      <a-form-item
        label="内容"
        :rules="[{ required: true, message: '标题不能为空' }]"
      >
        <WangEditor
          class="WangEditor"
          v-model:value="formModel.content"
          :maxHeight="300"
          @select="getRich"
        />
      </a-form-item>
    </a-form>
  </ant-modal>
</template>

<script setup lang="ts">
import { ref, watch, reactive } from "vue";
import { modalType } from "../../../interface/enum";
import useForm from "@/utils/useForm";
import WangEditor from "@/components/WangEditor.vue";
import user from "@/store/user";

const props = defineProps({
  width: {
    type: Number,
    default: () => 400,
  },
  visible: {
    type: Boolean,
    default: () => false,
  },
  loading: {
    type: Boolean,
    default: () => false,
  },
  type: {
    type: String,
    default: () => "",
  },
  data: {
    type: Object,
    default: () => ({}),
  },
  tagList: {
    type: Array<{ id: number; name: string; }>,
    default: () => ([]),
  },
});
const emit = defineEmits(["ok", "update:visible"]);

const title = ref<string>();
watch(
  () => props.type,
  () => {
    console.log(props.type);
    if (props.type == modalType.ADD) title.value = "新增";
    else if (props.type == modalType.EDIT) title.value = "编辑";
  },
  {
    deep: true,
    immediate: true,
  }
);

const formObj = reactive({
  title: undefined,
  content: undefined,
  description: undefined,
  type: undefined,
  tagIds: [],
});

const { formref, formModel } = useForm(formObj);

watch(
  () => props.data,
  () => {
    console.log(props.data);
    if (props.type == modalType.EDIT) {
      formModel.value = { ...formModel.value, ...props.data };
      if(props.data.tagIds) formModel.value.tagIds = JSON.parse(props.data.tagIds);
    }
  },
  {
    deep: true,
    immediate: true,
  }
);
//当编辑器的内容更新时，获取该值
const getRich = function (value: string) {
  formModel.value.content = value;
  console.log(value);
};
const cancel = () => {
  emit("update:visible");
};
const doConfirm = () => {
  console.log("进入提交");
  formref.value.validate().then(() => {
    const obj = {
      ...formModel.value,
      userId: user.userinfo?.userId,
      tagIds: JSON.stringify(formModel.value.tagIds)
    };
    console.log("要提交的数据：", obj);
    emit("ok", { type: props.type, obj });
  });
};
</script>
